const dbutil = require('../../../libs/MyDB');
module.exports = {

    //教师登录验证
    checkLogin: async function (data) {
        let result = {}
        await dbutil.singleSelect('study_teacher', 'password', `jobId='${data.sid}'`)
            .then(
                res => {
                    // console.log(res);
                    result = res
                }
            )
        if (!result.password) {
            return {
                code: 400,
                msg: "sid is not exist"
            }
        } else {
            if (result.password != (data.password)) {
                return {
                    code: 402,
                    msg: "password is wrong"
                }
            } else {
                let teacher = {};
                await dbutil.singleSelect('study_teacher', ['tname', 'school', 'class_name', 'jobId', 'teacher_id','avater', 'description'], `jobId='${data.sid}'`)
                    .then(
                        result => {
                            teacher = result;
                        }
                    )
                return {
                    code: 200,
                    msg: "login success",
                    teacher,
                }
            }
        }
    },
     //教师修改个人信息
     updateMsg: async function (data) {
        await dbutil.update('study_teacher', `jobId=${data.sid}`, {
            description: data.inputTextarea,
            avater: data.img,
        })
        let teacher = {};
        await dbutil.multiSelect('study_teacher', ['tname', 'school', 'class_name', 'jobId', 'description','avater'], `jobId='${data.sid}'`)
            .then(
                result => {
                    teacher = result[0];
                }
            )
        return {
            code: 200,
            msg: "修改成功",
            teacher,
        }
    },
    //渲染班级
    myclass: async function (teacherid) {
        let result = {};
        await dbutil.singleSelect('study_class', 'class_name', `teacher_id=${teacherid}`)
            .then(
                res => {
                    result = res;
                }
            )
        // console.log(result);
        return result;
    },

    //渲染所有学生
    allstudent: async function (teachername) {
        let result = {};
        await dbutil.multiSelect('study_studentinfo', ['studentId', 'uimg', 'realname', 'sex', 'class_name', 'user_id'], `tname="${teachername}"`)
            .then(
                res => {
                    result = res;
                }
            )
        console.log(result);
        return result;
    },

    //渲染部分学生
    somestudent: async function (classname) {
        let result = {};
        await dbutil.multiSelect('study_studentinfo', ['studentId', 'uimg', 'realname', 'sex', 'class_name', 'user_id'], `class_name="${classname}"`)
            .then(
                res => {
                    result = res;
                }
            )
        // console.log(result);
        return result;
    },

    //删除学生
    delstudent: async function (userid) {
        await dbutil.del('study_user', `user_id=${userid}`)

        await dbutil.del('study_userdetail', `user_id=${userid}`)
    },

    addClass: async function (classid, teacherid, textContent) {
        await dbutil.add('study_class', {
            // class_id: classid,
            teacher_id: teacherid,
            class_name: textContent
        })
    },

    //教师课程渲染
    techercourse: async function (jobId) {
        return await dbutil.multiSelect('study_teachercourse', '*', `jobId = ${jobId}`)
    },

    //课程删除数据
    deltechercourse: async function (courseid) {
        return await dbutil.del('study_teacher_course', `teachercoure_id=${courseid}`)
    },

    //添加课程
    addtechercourse: async function (ueseid, typeid, title, signup, img, type, teacherid) {
        return await dbutil.add({
            ueseid: ueseid,
            typeid: typeid,
            title: title,
            signup: signup,
            img: img,
            type: type,
            teacherid: teacherid,
        })
    }


}